package com.jxj.GUI;

import com.jxj.mapper.mapperUser;
import com.jxj.pojo.fankui;
import com.jxj.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;


public class setGUI {
    newmain n1 = new newmain();
    JButton queryButton = new JButton("查询");
    JButton chuliButton = new JButton("处理");
    private DefaultTableModel model = new DefaultTableModel(
            new Object[]{"反馈学生编号", "姓名", "反馈时间", "反馈内容", "处理状态"}, 0
    ) {
        @Override
        public boolean isCellEditable(int row, int column) {
            return false; // 禁止所有单元格编辑
        }
    };
    JTable table = new JTable(model);

    ArrayList<fankui> tingList = new ArrayList<>();
    private SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
    mapperUser mapperuser = sqlSession.getMapper(mapperUser.class);

    public void setGUI(JPanel setjpanel) {
        //学生反馈管理界面
        JPanel topsearch = new JPanel(new FlowLayout(FlowLayout.LEFT, 30, 10));
        JLabel shutu = new JLabel("反馈日期");
        n1.changeButton(chuliButton);
        n1.changeButton(queryButton);
        topsearch.add(shutu);
        //topsearch.add(datepick);
        topsearch.add(queryButton);
        topsearch.add(chuliButton);
        setjpanel.add(topsearch, BorderLayout.NORTH);
        setjpanel.add(table);
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setPreferredSize(new Dimension(1000, 500));
        scrollPane.setViewportView(table);
        setjpanel.add(scrollPane, BorderLayout.CENTER);
        queryButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    search();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        chuliButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if (row == -1) {
                    JOptionPane.showMessageDialog(
                            null, // 父组件，如果为 null，则弹窗会显示在屏幕中央
                            "当前没有选中任何行", // 提示消息
                            "提示", // 标题
                            JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                    );
                } else {
                    chuli();
                }
            }
        });
    }

    private void chuli() {
        chuliGUI c1 = new chuliGUI();
        c1.chuliWindow(table);
    }

    private void search() throws SQLException {
        newmodel(table);
        for (fankui student : mapperuser.fankuiList()) {
            model.addRow(new Object[]{student.getId(), student.getName(), student.getTime(), student.getThing(), student.getStatus()});
        }
    }

    private void newmodel(JTable table) {
        table.setRowHeight(30);
        model = new DefaultTableModel(
                new Object[]{"反馈学生编号", "姓名", "反馈时间", "反馈内容", "处理状态"}, 0
        ) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 禁止所有单元格编辑
            }
        };
        table.setModel(model);
    }
}
